/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketmaster.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.RowSet;
import ticketmaster.dataobject.Bankcard;

/**
 *
 * @author Varadha
 */
public class BankcardDAO extends BaseEntityDAO{

    public BankcardDAO()
    {
        // initialization
    }

    /**
     * Insert Bankcard into database
     * @param Bankcard the new Bankcard entity to be inserted
     * @return newly created Bankcard number or a 0 on error
     */
    public int insertBankcard(Bankcard bankcard)
    {
        return save(bankcard);
    }

    /**
     * Delete bankcard that matches the supplied criteria
     * @param bankcard the supplied criteria
     * @return true on success, false on failure
     */
    public boolean deleteBankcard(Bankcard bankcard)
    {
        return delete(bankcard);
    }

    /**
     * Update the bankcard using data from the bankcard Data Object
     * @param bankcard the bankcard Data Object to be updated
     * @return true on success, false on failure or error
     */
    public boolean updateBankcard(Bankcard bankcard)
    {
        if (bankcard.getBankCardID() == 0)
        {
            return false;
        }

        if (update(bankcard))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /**
     * Get the bankcard that matches the supplied criteria
     * @param bankcard the supplied criteria
     * @return a Data Object if found, or null on error or if not found
     */
    public Bankcard findBankcard(Bankcard bankcard)
    {
        String sql = null;

        //if(bankcard.getBankCardID() !=0)
            sql = buildQuerySQL(bankcard);
        //else
            //return null;

        RowSet crs = query(sql);
        try
        {
            crs.next();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(BankcardDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return (Bankcard) this.getObject(crs, bankcard);
    }

    /**
     * Get all bankcards that match the supplied criteria
     * @param bankcard supplied criteria
     * @return a RowSet
     */
    public RowSet getBankcardsRS(Bankcard bankcard)
    {
        String sql = buildQuerySQL(bankcard);
        return query(sql);
    }

    /**
     * Get all bankcards that match the supplied criteria
     * @param bankcard supplied criteria
     * @return a collection of Bankcard Data Objects
     */
    public Collection<Bankcard> getBankcardsDO(Bankcard bankcard)
    {
        String sql = buildQuerySQL(bankcard);

        RowSet crs = query(sql);
        Collection<Bankcard> bankcards = new ArrayList<Bankcard>();
        try
        {
            while (crs.next())
            {
                Bankcard bankcardDO = (Bankcard) this.getObject(crs, bankcard);
                bankcards.add(bankcardDO);
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(BankcardDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return bankcards;
    }


}
